/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.hymnal.church;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;


import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.io.Writer;

import net.hymnal.church.TableManager.TableItem;


/**
 *
 * @author kvsevolo
 */
public class HymnsReader {

   
    private String pathCommon = null;

    HymnsReader(String pathCommonArg) {
        pathCommon = pathCommonArg;
        //tableHymns = new TreeMap();
    }

    public void readHymnsRefactorOnly() {
        int countFiles = new File(pathCommon).list().length;
        for (int i = 0; i < countFiles; i++) {
            String inFullFileName = pathCommon + "r" + Integer.toString(i + 1) + ".txt";
            String outFullFileName = "E:/prj_17march/trunk/assets/common_gen/r" + (i + 1) + ".txt";
            System.out.println(inFullFileName);
                        
            
            Reader reader = null;
            try {
                try {
                    reader = new InputStreamReader(new FileInputStream(inFullFileName), "Windows-1251");
                } catch (FileNotFoundException ex) {
                    System.out.println("Error.");
                }
            } catch (UnsupportedEncodingException ex) {
                System.out.println("Error.");
            }
            BufferedReader br = new BufferedReader(reader);
            
            Writer writer = null;
            try {
                try {
                    writer = new OutputStreamWriter(new FileOutputStream(outFullFileName), "Windows-1251");
                } catch (FileNotFoundException ex) {
                    System.out.println("Error.");
                }
            } catch (UnsupportedEncodingException ex) {
                System.out.println("Error.");
            }
            
            BufferedWriter bw = new BufferedWriter(writer);

            /*
            //InputStream is = new FileInputStream(f);
            FileReader fr;
            FileWriter fw = null;
            try {
                fr = new FileReader(fr);
                try {
                    fw = new FileWriter(fOut);
                } catch (IOException ioe) {
                    System.out.println("ioe: File:" + fullFileName + " not found.");
                }
            } catch (FileNotFoundException ex) {
                System.out.println("File:" + fullFileName + " not found.");
                continue;
            }
            BufferedWriter bw = new BufferedWriter(fw);
            BufferedReader br = new BufferedReader(fr);
            * */
            String line;
            TableItem item = TableManager.createTableItem();
            TableManager.tableHymns.put(i + 1, item);
            item.commonNum = i + 1;
            int index = -1;
            int lineNum = 0;
            try {
                while ((line = br.readLine()) != null) {
                    String outLine = "";
                    lineNum++;
                    
                    int charIndex = line.indexOf(" A");
                      
                    if (charIndex >= 0) {
                        if (lineNum > 5) {
                            outLine = line;
                        } else {
                            outLine = line.substring(0, charIndex) + ")";
                        }
                    } else {
                        outLine = line;
                    }
                    try {
                        bw.write(outLine);
                        bw.newLine();
                        bw.flush();
                    } catch (IOException ioe) {
                        System.out.println("ioe, bw.write(outLine);" + ioe.getMessage());
                    }
//                    if(line.contains("1.")) {
//                        item.firstLine = br.readLine();
//                        if(item.firstLine == null) {
//                            System.out.println("Bad hymn format, please review hymn:" + (i+1));
//                            break;
//                        }                        
//                    }
//                    index = line.indexOf("(A");
//                    if(index >= 0) {  
//                        index += 2;                        
//                        String numStr;
//                        for(int ii = 4; ii > 0; ii-- ) {
//                            numStr = line.substring(index, index + ii);
//                            try {
//                                item.englishNum = Integer.parseInt(numStr);
//                            } catch (NumberFormatException nfe) {
//                                continue;
//                            }
//                            break;
//                        }
//                        break;
//                    }
                }
                bw.close();

            } catch (IOException ioe) {
                System.out.println("ioe: " + ioe.getMessage());
                continue;//for FOR.
            }
            if (index == -1) {
                System.out.println("English number for hymn: " + (i + 1) + " not found.");
                continue;
            }
            System.out.println("English number for: " + (i + 1) + "is: " + item.englishNum);
        }
    }
    void readHymns() throws IOException {        
        String fileList[] = new File(pathCommon).list();
        for (int i = 0; i < fileList.length; i++) {
        //for (int i = 0; i < 5; i++) {
            String inFullFileName = pathCommon + "\\r" + Integer.toString(i + 1) + ".txt";            
            //String inFullFileName = pathCommon + "\\" + fileList[i];
            Reader reader = null;
            try {
                try {
                    reader = new InputStreamReader(new FileInputStream(inFullFileName), "Windows-1251");
                } catch (FileNotFoundException ex) {
                    throw new IOException("Wrong file name : " + inFullFileName);
                }
            } catch (UnsupportedEncodingException ex) {
                System.out.println("Error.");
            }
            BufferedReader br = new BufferedReader(reader);            
            
            String line;
            TableItem item = TableManager.createTableItem();
            TableManager.tableHymns.put(i + 1, item);
            item.commonNum = i + 1;
            int index = -1;
            int lineCounter = 0;
            item.mainTheme = br.readLine();
            item.subTheme  = br.readLine();
            try {
                while ((line = br.readLine()) != null) {
                    if (line.contains("1.")) {
                        item.firstLine = br.readLine();
                        if (item.firstLine == null) {
                            System.out.println("Bad hymn format, please review hymn:" + (i + 1));
                            break;
                        }
                    }
                    index = line.indexOf("(A");
                    if (index >= 0) {
                        index += 2;
                        String numStr;
                        for (int ii = 4; ii > 0; ii--) {
                            try {
                                numStr = line.substring(index, index + ii);
                            } catch (StringIndexOutOfBoundsException sioobex) {
                                continue;
                            }
                            try {
                                item.englishNum = Integer.parseInt(numStr);
                            } catch (NumberFormatException nfe) {
                                continue;
                            }
                            break;
                        }                        
                    }
                }           

            } catch (IOException ioe) {
                System.out.println("ioe: " + ioe.getMessage());
                continue;//for FOR.
            }
            if (index == -1) {
                //System.out.println("English number for hymn: " + (i + 1) + " not found.");
                //continue;
            } else {
                //System.out.println("English number for: " + (i + 1) + "is: " + item.englishNum);
            }            
        }    
    }
}
